package com.github.crush0.myblog.controller;

import com.github.crush0.myblog.Exception.ServiceException;
import com.github.crush0.myblog.util.IPUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class HomeController extends BaseController{
    @GetMapping("/")
    public String IndexPage(HttpServletRequest request, HttpSession session){
        String UserIp = IPUtils.getIpAddr(request);
        session.setAttribute("ip",UserIp);
        return "index";
    }

    @GetMapping("/edit")
    public String EditPage(HttpServletResponse response, HttpSession session){
        try {
            String userIp = (String) session.getAttribute("ip");
            if (userIp.equals("127.0.0.1")) {
                return "html/edit";
            } else {
                response.sendRedirect("/");
            }
        }
        catch (Exception ex){
            throw new ServiceException("重定向异常");
        }
        return "index";
    }
}
